package com.citicpub_cloud.restfull.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.DownloadFileRequest;
import com.aliyun.oss.model.DownloadFileResult;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;

@Component
public class OSSManageUtil implements ApplicationContextAware{
	private static Logger log = LoggerFactory.getLogger(OSSManageUtil.class);
	
	private static OSSConfigure ossConfigure = null; 
	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		// TODO Auto-generated method stub
		ossConfigure = (OSSConfigure) applicationContext.getBean("ossConfigure");  
	}
	
    public static String getImgPublicReadUrl(String key){
    	String returnstr = "";
    	if(key.startsWith("https://")||key.startsWith("http://")){
    		returnstr = key;
		}else {
			returnstr = UploadUtils.combinePath(ossConfigure.getImgAccessUrl(), key);
		}
    	return returnstr;
    }
    
    public static String getOtherPublicReadUrl(String key){
    	String returnstr = "";
    	if(key.startsWith("https://") ||key.startsWith("http://")){
    		return key;
		}else {
			returnstr = UploadUtils.combinePath(ossConfigure.getOtherAccessUrl(), key);
			return returnstr;
		}
    } 
    
    
    public static byte[] downloadImgRemotFile(String remoteUrl){
    	try {
			byte[] buff = DefaultTrustManagerUtil.doGet(getImgPublicReadUrl(remoteUrl));
			return buff;
		} catch (Exception e) {
			log.error("远程文件保存失败：" + remoteUrl, e);
			return null;
		}
    }
    
    public static byte[] downloadOtherRemoteFile(String remoteUrl){
    	try {
			byte[] buff = DefaultTrustManagerUtil.doGet(getOtherPublicReadUrl(remoteUrl));
			return buff;
		} catch (Exception e) {
			log.error("远程文件保存失败：" + remoteUrl, e);
			return null;
		}
	}
    
    
    public static Long getContentLength(String key) {
		OSSClient ossClient = null;
	    try {
			ossClient = new OSSClient(ossConfigure.getAccountendpoint(), ossConfigure.getAccesskeyid(),  
					ossConfigure.getAccesskeysecret());
			
			if (null != ossClient) {
				ObjectMetadata objectMetadata = ossClient.getObjectMetadata(ossConfigure.getBucketName(), key);
				if (null != objectMetadata) {
					return objectMetadata.getContentLength();
				}
			}
	    } catch (Exception e) {
	      e.printStackTrace();
	    } finally {
	      if (null != ossClient) {
	        try {
	          // 关闭OSSClient
	          ossClient.shutdown();
	        } catch (Exception e) {
	          // TODO Auto-generated catch block
	          e.printStackTrace();
	        }
	      }
	    }
	    return 0l;
	
	}
    
    public static void downloadFile1(String key, String filename){
    	OSSClient ossClient = null;
        
    	try {
    		ossClient = new OSSClient(ossConfigure.getAccountendpoint(), ossConfigure.getAccesskeyid(),  
    				ossConfigure.getAccesskeysecret());
    		
	    	if(null != ossClient){	
	    		DownloadFileRequest downloadFileRequest = new DownloadFileRequest(ossConfigure.getBucketName(), key);
	    		downloadFileRequest.setDownloadFile(filename);
	    		downloadFileRequest.setPartSize(5 * 1024 * 1024);
	    		downloadFileRequest.setTaskNum(10);
	    		downloadFileRequest.setEnableCheckpoint(true);
	    		
	    		DownloadFileResult downloadRes = ossClient.downloadFile(downloadFileRequest);	    		
	    		downloadRes.getObjectMetadata();
    		}
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  finally{
			if(null != ossClient){					
	            try {
	            	// 关闭OSSClient  
		            ossClient.shutdown();  
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}  
			}
	       
	    } 
    } 
}
